package com.syezon.wifikey.setting;


import com.blankj.utilcode.util.SPUtils;
import com.syezon.wifikey.utils.DateUtil;
import com.syezon.wifikey.utils.MarketUtils;
import com.syezon.wifikey.utils.Utils;

import java.util.Date;

public class GradeConfig {

    private static final String CONFIG_NAME = "battery_grade_config";
    private static final String HAS_ENCOURAGE = "has_encourage";
    private static final String LAST_SHOW_TIME = "last_show_time";
    private static final String FIRST_OPEN_APP_TIME_STAMP = "first_open_app_time_stamp";
    private static final String CAN_SHOW_GRADE_DIALOG = "can_show_grade_dialog";
    private static final String LAST_SHOW_DAY = "last_show_show";


    private static final int INTERVAL_DAYS = 3;


    public static void saveHasEncourage(boolean encourage) {
        SPUtils.getInstance(CONFIG_NAME).put(HAS_ENCOURAGE, encourage);
    }

    public static boolean hasEncourage() {
        return SPUtils.getInstance(CONFIG_NAME).getBoolean(HAS_ENCOURAGE, false);
    }

    public static long lastShowTime() {
        return SPUtils.getInstance(CONFIG_NAME).getLong(LAST_SHOW_TIME, 0);
    }

    public static void saveLastShowTime() {
        long currentTimeMillis = System.currentTimeMillis();
        SPUtils.getInstance(CONFIG_NAME).put(LAST_SHOW_TIME, currentTimeMillis);
    }

    public static void saveFirstOpenAppTimeStamp() {
        long currentTimeMillis = System.currentTimeMillis();
        SPUtils.getInstance(CONFIG_NAME).put(FIRST_OPEN_APP_TIME_STAMP, currentTimeMillis);
    }

    public static long getFirstOpenAppTimeStamp() {
        return SPUtils.getInstance(CONFIG_NAME).getLong(FIRST_OPEN_APP_TIME_STAMP, 0);
    }

    public static boolean canShowGradeDialog() {
        return SPUtils.getInstance(CONFIG_NAME).getBoolean(CAN_SHOW_GRADE_DIALOG, true);
    }

    public static void saveCanShowGradeDialog(boolean canShow) {
        SPUtils.getInstance(CONFIG_NAME).put(CAN_SHOW_GRADE_DIALOG, canShow);
    }

    public static void saveLastShowDayInt() {
        int dayInt = DateUtil.getDayInt(new Date());
        SPUtils.getInstance(CONFIG_NAME).put(LAST_SHOW_DAY, dayInt);
    }

    public static int getLastShowDayInt() {
        return SPUtils.getInstance(CONFIG_NAME).getInt(LAST_SHOW_DAY, 0);
    }


    public static boolean canShowGradeNew() {
        long timeStamp = getFirstOpenAppTimeStamp();
        Date openDate = new Date(timeStamp);
        Date secondDay = DateUtil.addDay(openDate, 1);
        Date fourthDay = DateUtil.addDay(openDate, 3);
        Date sixDay = DateUtil.addDay(openDate, 5);
        Date currentDate = new Date();
        int currentDayInt = DateUtil.getDayInt(currentDate);
        int secondDayInt = DateUtil.getDayInt(secondDay);
        int fourthDayInt = DateUtil.getDayInt(fourthDay);
        int sixDayInt = DateUtil.getDayInt(sixDay);
        if (currentDayInt == secondDayInt || currentDayInt == fourthDayInt || sixDayInt == currentDayInt) {
            int lastShowDayInt = getLastShowDayInt();
            if (canShowGradeDialog() && lastShowDayInt != currentDayInt) {
                return true;
            }
        }
        return false;
    }

    public static boolean canShowGrade() {
        boolean canJump = MarketUtils.canJump(Utils.getApp());
        if (canJump) {
            long lastShowTime = lastShowTime();
            long showInterval = System.currentTimeMillis() - lastShowTime;
            int intervalDays = (int) (showInterval / 1000 / 60 / 60 / 24);
            if (intervalDays >= INTERVAL_DAYS) {
                return true;
            }
        }
        return false;
    }

    public static boolean canJumpMarket() {
        return MarketUtils.canJump(Utils.getApp());
    }
}
